
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World: World;
float4x4 WorldInverseTranspose : WorldInverseTranspose;
float4x4 ViewInverse : ViewInverse;
float4x4 Proj : Projection;

float4 MaterialAmbient: Ambient;
float4 MaterialDiffuse: Diffuse;
float4 MaterialSpecular: Specular;

float4 LightPosition0 : LightPosition0;
//float4 LightPosition0 : Position <    string Object = "Pointlight";    string Space = "World";> = {1.0f, 1.0f, -1.0f, 0.0f};
float4 LightDiffuse0 : LightDiffuse0;
//float4 LightDiffuse0 : Diffuse  <    string Object = "Pointlight";>;
float4 LightSpecular0 : LightSpecular0;
//float4 LightSpecular0 : Specular  <    string Object = "Pointlight";>;
float3 LightAttenuation0: LightAttenuation0;

float4 LightPosition1 : LightPosition1;
//float4 LightPosition1 : Position <    string Object = "Pointlight";    string Space = "World";> = {1.0f, 1.0f, -1.0f, 0.0f};
float4 LightDiffuse1 : LightDiffuse1;
//float4 LightDiffuse1 : Diffuse  <    string Object = "Pointlight";>;
float4 LightSpecular1 : LightSpecular1;
//float4 LightSpecular1 : Specular  <    string Object = "Pointlight";>;
float3 LightAttenuation1: LightAttenuation1;
//float3 LightAttenuation1: Attenuation <    string Object = "Pointlight";>;

float4 LightPosition2 : LightPosition2;
//float4 LightPosition2 : Position <    string Object = "Pointlight";    string Space = "World";> = {1.0f, 1.0f, -1.0f, 0.0f};
float4 LightDiffuse2 : LightDiffuse2;
//float4 LightDiffuse2 : Diffuse  <    string Object = "Pointlight";>;
float4 LightSpecular2 : LightSpecular2;
//float4 LightSpecular2 : Specular  <    string Object = "Pointlight";>;
float3 LightAttenuation2: LightAttenuation2;
//float3 LightAttenuation2: Attenuation <    string Object = "Pointlight";>;

float Shininess: Shininess;

float Bumpy: FloatParameter0 = 1;

struct VS_INPUT{
	float3 Position: POSITION;
	float4 UV: TEXCOORD0;
	float3 Normal: NORMAL;
	float3 Tangent: TANGENT;
	float3 Binormal: BINORMAL;
};
struct VS_OUTPUT{
    float4 pos:POSITION;
    float4 UV: TEXCOORD0;
    float3 WorldNormal: TEXCOORD1;
    float3 WorldPosition: TEXCOORD2;
    float3 WorldTangent: TEXCOORD3;
    float3 WorldBinormal: TEXCOORD4;
    
    
};

texture BaseTexture: TEXTURE0;
texture NormalTexture: TEXTURE1;

sampler2D BaseSampler = sampler_state
{
    Texture = (BaseTexture);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = WRAP;
	ADDRESSV=WRAP;
};

sampler2D NormalSampler = sampler_state
{
    Texture = (NormalTexture);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = WRAP;
	ADDRESSV=WRAP;
};

VS_OUTPUT mainVS(VS_INPUT input){
    VS_OUTPUT output;
    output.pos=mul(float4(input.Position, 1.0), WorldViewProj);
    output.UV=input.UV;
    output.WorldNormal =  mul(input.Normal, WorldInverseTranspose);
    output.WorldTangent =  mul(input.Tangent, WorldInverseTranspose);
    output.WorldBinormal =  mul(input.Binormal, WorldInverseTranspose);
    output.WorldPosition =  mul(float4(input.Position, 1.0), World);
	
	return output;
}

float4 mainPS(VS_OUTPUT IN) : COLOR {
	// Lighting
	float3 DirectionToCamera = normalize(ViewInverse[3].xyz - IN.WorldPosition);

 	float3 Bumps = Bumpy * tex2D(NormalSampler, IN.UV).xyz - 0.5f;
	float3 Nn = normalize(IN.WorldNormal);
    float3 Tn = normalize(IN.WorldTangent);
    float3 Bn = normalize(IN.WorldBinormal);
    
    float3 Normal = Nn + (Bumps.x * Tn + Bumps.y * Bn);
    
    half3 VectorToLight0 = LightPosition0 - IN.WorldPosition;
	half3 DirectionToLight0 = normalize(VectorToLight0);
	half DistanceToLight0 = length(VectorToLight0);
	half LightPower0 = 1/(LightAttenuation0.x+LightAttenuation0.y*DistanceToLight0+LightAttenuation0.z*DistanceToLight0*DistanceToLight0);
	half  DiffuseIntensity0 = saturate(dot(DirectionToLight0, Normal))*LightPower0;
	half3 ReflectionVector0 = normalize(reflect(-DirectionToLight0, Normal));
	half SpecularIntensity0 = pow(max(0,dot(ReflectionVector0,DirectionToCamera)),Shininess)*LightPower0;
	
	half4 Diffuse0 = LightDiffuse0 * DiffuseIntensity0;
	half4 Specular0 = LightSpecular0 * SpecularIntensity0;

	half3 VectorToLight1 = LightPosition1 - IN.WorldPosition;
	half3 DirectionToLight1 = normalize(VectorToLight1);
	half DistanceToLight1 = length(VectorToLight1);
	half LightPower1 = 1/(LightAttenuation1.x+LightAttenuation1.y*DistanceToLight1+LightAttenuation1.z*DistanceToLight1*DistanceToLight1);
	half  DiffuseIntensity1 = saturate(dot(DirectionToLight1, Normal))*LightPower1;
	half3 ReflectionVector1 = normalize(reflect(-DirectionToLight1, Normal));
	half SpecularIntensity1 = pow(max(0,dot(ReflectionVector1,DirectionToCamera)),Shininess)*LightPower1;
	
	half4 Diffuse1 = LightDiffuse1 * DiffuseIntensity1;
	half4 Specular1 = LightSpecular1 * SpecularIntensity1;
	
	half3 VectorToLight2 = LightPosition2 - IN.WorldPosition;
	half3 DirectionToLight2 = normalize(VectorToLight2);
	half DistanceToLight2 = length(VectorToLight2);
	half LightPower2 = 1/(LightAttenuation2.x+LightAttenuation2.y*DistanceToLight2+LightAttenuation2.z*DistanceToLight2*DistanceToLight2);
	half DiffuseIntensity2 = saturate(dot(DirectionToLight2, Normal))*LightPower2;
	half3 ReflectionVector2 = normalize(reflect(-DirectionToLight2, Normal));
	half SpecularIntensity2 = pow(max(0,dot(ReflectionVector2,DirectionToCamera)),Shininess)*LightPower2;
	
	half4 Diffuse2 = LightDiffuse2 * DiffuseIntensity2;
	half4 Specular2 = LightSpecular2 * SpecularIntensity2;
	
	float4 DiffuseColor = tex2D(BaseSampler,IN.UV);
	float4 DiffuseComponent = DiffuseColor*((Diffuse0+Diffuse1+Diffuse2)*MaterialDiffuse+MaterialAmbient);
	float4 SpecularComponent = (Specular0+Specular1+Specular2)*MaterialSpecular;
	
    float3 finalColor = (DiffuseComponent+SpecularComponent).xyz;
	
	return float4(finalColor,DiffuseColor.a);	
}

technique Specular3Lights {
	pass p0 {
		ZEnable=true;
		ZWriteEnable=true;
		AlphaTestEnable = true;
		AlphaRef = 0.1f;
		AlphaBlendEnable = true;
		//FillMode = Wireframe;
		SRCBLEND = SRCALPHA;
		DESTBLEND = INVSRCALPHA;

		VertexShader = compile vs_3_0 mainVS();
		PixelShader = compile ps_3_0 mainPS();
	}
}
